package com.service;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dao.IXplstworDao;
import com.icim.service.ICIMBaseService;
import com.model.Ret_wo;
import com.tx.Xplstwor.XplstworI;
import com.tx.Xplstwor.XplstworO;
import com.tx.Xplstwor.XplstworOA;
import com.util.MqOpe;

@Scope("prototype")
@Service("XplstworService")
public class XplstworService extends ICIMBaseService{
	private Logger logger = Logger.getLogger(XplstworService.class);
	@Autowired
	private XplstworI xplstworI;
	
	@Autowired
	private XplstworO xplstworO; 
	
	
	@Autowired
	private Ret_wo ret_wo;
	
	@Autowired
	private IXplstworDao lstWorDao;
	
	@Autowired
	private MqOpe mqOpe;
	
	public String subMainProc(String strInMsg){
		List<Ret_wo> retWorList = new ArrayList<Ret_wo>();
		xplstworI = (XplstworI) mqOpe.inStr2Obj(strInMsg,xplstworI);
		logger.info("InTrx is "+ strInMsg );
		retWorList = this.findAllWorList();
		xplstworO = this.setOutTrx( retWorList );
		logger.info(mqOpe.obj2Str(xplstworO));
		return mqOpe.obj2Str(xplstworO);
	}
	
	public List<Ret_wo>findAllWorList(){
//		List<Ret_wo> retWoList = new ArrayList<Ret_wo>();
		String querySql=" from Ret_wo ";
		List<Ret_wo> retWoList =lstWorDao.find( querySql );
		return retWoList ;
	}
	
	public XplstworO setOutTrx( List<Ret_wo> retWorList ){
		XplstworO xplstworO = new XplstworO();
		xplstworO.setTrx_id("XPLSTPRD");
		xplstworO.setType_id("O");
		xplstworO.setRtn_code("0000000");
		xplstworO.setRtn_mesg("success");
		
		List<XplstworOA> oaryList = new ArrayList<XplstworOA>();
		for(Ret_wo retWo: retWorList){
			XplstworOA xplstworOA = new XplstworOA();
			xplstworOA.setWo_id(retWo.getWo_id());
			xplstworOA.setWo_dsc(retWo.getWo_dsc());
			xplstworOA.setWo_cate(retWo.getWo_cate());
			xplstworOA.setWo_stat(retWo.getWo_stat());
			xplstworOA.setSo_id(retWo.getSo_id());
			xplstworOA.setMdl_id_fk(retWo.getMdl_id_fk());
			xplstworOA.setPln_stb_date(retWo.getPln_stb_date().toString());
			xplstworOA.setPln_cmp_date(retWo.getPln_cmp_date().toString());
			xplstworOA.setPln_prd_qty(retWo.getPln_prd_qty());
			xplstworOA.setRl_prd_qty(retWo.getRl_prd_qty());
			xplstworOA.setDest_shop(retWo.getDest_shop());
			xplstworOA.setPlant_id(retWo.getPlant_id());
			xplstworOA.setVcr_flg_fk(retWo.getVcr_flg_fk());
			oaryList.add(xplstworOA);

		}
		xplstworO.setOary(oaryList);
		xplstworO.setTbl_cnt(String.valueOf( oaryList.size()));
		return xplstworO;
	}
	
}
